package com.ww.companyCabinetSystem.constant;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 线程池.
 * @author zidu
 *2015年12月21日
 */
public final class ThreadPool {
	/**.
	 * threadPool 线程池实例
	 */
	private static volatile ThreadPool threadPool;
	
	/**
	 * executorService 线程管理器.
	 */
	private ExecutorService pool;
	
	/**
	 * 构造函数.
	 *
	 */
	private ThreadPool(){
		if (pool == null) {
			pool = Executors.newFixedThreadPool(BaseConstant.THREADPOOL_SIZE);
		}
	}
	
	/**
	 * 获取实例.
	 * @return
	 */
	public static ThreadPool getInstance() {
		if (threadPool == null) {
			synchronized (ThreadPool.class) {
				if (threadPool == null) {
					threadPool = new ThreadPool();
				}
			}
		}
		return threadPool;
	}
	
	 /**
     * 统计批次情况. 加载任务,将任务加载到线程池任务队列中.
     *
     * @param task
     *            加载的任务runnable
     */
    public synchronized void loadTasks(Runnable task) {
        // 加入任务
        pool.execute(task);
    }

    /**
     * 任务完加载完以后关闭线程池，不接受新任务.
     */
    public synchronized void shutdownPool() {
        if (pool != null){
            pool.shutdown();
        }
    }
}
